class_name EntityExt
extends Entity
# fighter data it definded in _init before createEntity
export var data := {hp = 2, max_hp = 10, camp = 1, skills = [100], sprite = "bear"}
onready var hp_bar = $HpBar
onready var tween = $Tween


# Called when the world ready
func setup(query:QueryManager):
	var hp_m = query.fetch_component("HpC") as Multi
	var ref = funcref(self, "update_hp")
	hp_m.add_modify_listener(ref, [hp_m])


func update_hp(e, read: Array):
	if e.id != self.id:
		return
	var hp_m = read[0]
	var hp = hp_m.get(e.id) as HpC
	var percent = float(hp.value) / hp.max_hp * 100
	var old_percent = float(hp.old_hp) / hp.max_hp * 100
	var duration := abs(percent - old_percent) / 100 * 1.0
	tween.interpolate_property(hp_bar, "value", old_percent, percent,duration,Tween.TRANS_QUAD)
	tween.start()
	#hp_bar.value = int(percent)
